package com.xbdun.service.bmp.facade;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.xbdun.common.api.Client;
import com.xbdun.common.api.XbdUnStringResult;
import com.xbdun.common.enums.UserRolesEnums;
import com.xbdun.common.util.CommonUtil;
import com.xbdun.facade.bmp.exceptions.BmpBizException;
import com.xbdun.facade.bmp.request.system.OrleDepartPagerRequest;
import com.xbdun.facade.bmp.request.system.OrleDepartRequest;
import com.xbdun.facade.bmp.result.system.OrleDepartPagerResult;
import com.xbdun.facade.bmp.service.system.OrleDepartFacade;
import com.xbdun.service.bmp.service.system.OrleDepartService;

import lombok.extern.slf4j.Slf4j;

//机构部门
@Component
@Slf4j
public class OrleDepartFacadeImpl implements OrleDepartFacade{

	@Autowired
	OrleDepartService orleDepartService;
	public XbdUnStringResult addDepartRoles(OrleDepartRequest request, Client client) {
		XbdUnStringResult result = new XbdUnStringResult();
		try{
			log.info("start - 部门关联角色新增初始参数,{}", client);
			
			CommonUtil.validateClient(client);
			if(null==request.getDepartCode()||request.getDepartCode().longValue()<=0){
				throw BmpBizException.PARAM_CHKE_FAIL.newInstance("部门编号不能为空");
			}
			if(CollectionUtils.isEmpty(request.getRoleId())){
				throw BmpBizException.PARAM_CHKE_FAIL.newInstance("角色Id不能为空");
			}
			//如果是特殊角色，那么能够设置只能够是超级管理员和平台管理员
			if(request.getBlspacialRole()){
				if(!(UserRolesEnums.isSupperAdmin(client.getRoleId())||UserRolesEnums.isXbdAdmin(client.getRoleId()))){
					throw BmpBizException.PARAM_CHKE_FAIL.newInstance("权限不够，只能够为超级管理员和平台管理员才可以设置;");
				}
			}
			//新增部门关联角色
			orleDepartService.addDepartRoles(request,client);
		} catch (Exception ex) {
			result.setBizException(ex);
			log.error("部门关联角色新增,入参:{},异常:{}", client, ex);
		} finally {
			log.info("end - 部门关联角色新增结果:{}", result);
		}
		return result;
	}
	
	public OrleDepartPagerResult queryOrleDepartOrg(OrleDepartPagerRequest request,Client client) {
		OrleDepartPagerResult result = new OrleDepartPagerResult();
		try{
			log.info("start - 查询当前机构授权的部门关联角色:" + request);
			
			CommonUtil.validateClient(client);
			result.setData(orleDepartService.queryOrleDepartOrg(request, client));
		}catch(Exception e){
			result.setBizException(e);
			log.error("查询当前机构授权的部门关联角色，入参{},异常:{}",JSON.toJSONString(request),e);
		}finally{
			log.info("end - 查询当前机构授权的部门关联角色结果:" + result);
		}
		return result;
	}

	public XbdUnStringResult delDepartRole(Long id, Client client) {
		XbdUnStringResult result = new XbdUnStringResult();
		try{
			log.info("start - 部门关联角色新增初始参数,{}", client);
			
			CommonUtil.validateClient(client);
			if(null==id||id.longValue()<=0){
				throw BmpBizException.PARAM_CHKE_FAIL.newInstance("选项不能为空");
			}
			//新增部门关联角色
			orleDepartService.delDepartRole(id,client);
		} catch (Exception ex) {
			result.setBizException(ex);
			log.error("部门关联角色新增,入参:{},异常:{}", client, ex);
		} finally {
			log.info("end - 部门关联角色新增结果:{}", result);
		}
		return result;
	}
}
